package com.master.demo.sys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.master.common.api.IPageInfo;
import com.master.common.api.Query;
import com.master.common.api.ResponseInfo;
import com.master.common.exception.ErrorException;
import com.master.common.utils.DozerUtil;
import com.master.common.validator.ValidatorUtil;
import com.master.demo.sys.mapper.AdvertisingMapper;
import com.master.demo.sys.pojo.command.AdvertisingCommand;
import com.master.demo.sys.pojo.entity.Advertising;
import com.master.demo.sys.pojo.vo.AdvertisingVO;
import com.master.demo.sys.service.IAdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  广告位配置 服务实现类
 *
 * @author: Yang
 * @date: 2023-06-15
 * @version: 1.0.0
 * Copyright Ⓒ 2022 Master Computer Corporation Limited All rights reserved.
 */
@Service
public class AdvertisingServiceImpl extends ServiceImpl<AdvertisingMapper, Advertising> implements IAdvertisingService {
    @Autowired
    private AdvertisingMapper advertisingMapper;

    /**
     * 根据id获取广告位配置信息
     *
     * @param id 广告位配置id
     * @return
     */
    private Advertising getAdvertising(Long id) {
        Advertising advertising = advertisingMapper.selectById(id);
        if (ValidatorUtil.isNull(advertising)) {
            throw new ErrorException("广告位配置信息不存在，无法操作");
        }
        return advertising;
    }

    @Override
    public ResponseInfo<IPageInfo<AdvertisingVO>> pageAdvertising(Query query) {
        IPage<AdvertisingVO> iPage = advertisingMapper.pageAdvertising(new Page<>(query.getCurrent(), query.getSize()), query);
        return ResponseInfo.success(new IPageInfo(iPage.getCurrent(), iPage.getSize(), iPage.getTotal(), iPage.getRecords()));
    }

    @Override
    public ResponseInfo<List<AdvertisingVO>> listAdvertising(Query query) {
        return ResponseInfo.success(advertisingMapper.listAdvertising(query));
    }

    @Override
    public ResponseInfo<AdvertisingVO> getAdvertisingById(Long id) {
        return ResponseInfo.success(DozerUtil.convertor(getAdvertising(id), AdvertisingVO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "masterTransactionManager")
    public ResponseInfo saveAdvertising(AdvertisingCommand command) {
        Advertising advertising = DozerUtil.convertor(command, Advertising.class);
        advertising.setCreateUser(command.getOperater());
        advertisingMapper.insert(advertising);
        return ResponseInfo.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "masterTransactionManager")
    public ResponseInfo updateAdvertising(AdvertisingCommand command) {
        Advertising advertising = getAdvertising(command.getId());
        DozerUtil.convertor(command, advertising);
        advertising.setUpdateUser(command.getOperater());
        advertising.setUpdateTime(LocalDateTime.now());
        advertisingMapper.updateById(advertising);
        return ResponseInfo.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "masterTransactionManager")
    public ResponseInfo removeAdvertisingByIds(List<Long> ids) {
        advertisingMapper.deleteBatchIds(ids);
        return ResponseInfo.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class, transactionManager = "masterTransactionManager")
    public ResponseInfo removeAdvertisingById(Long id) {
        advertisingMapper.deleteById(id);
        return ResponseInfo.success();
    }

}
